package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.CmsSiteBanner;
import com.ruoyi.cms.domain.CmsSiteInfo;
import com.ruoyi.cms.domain.CmsSiteLy;
import com.ruoyi.cms.domain.CmsSiteModule;
import com.ruoyi.cms.service.ICmsSiteBannerService;
import com.ruoyi.cms.service.ICmsSiteInfoService;
import com.ruoyi.cms.service.ICmsSiteLyService;
import com.ruoyi.cms.service.ICmsSiteModuleService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.*;

import static com.ruoyi.common.utils.PageUtils.startPage;

@Controller
public class CmsWebController extends BaseController {
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;

    @Autowired
    private ICmsSiteLyService cmsSiteLyService;

    private CmsSiteInfo handPubData(Model model) {
        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteModule head = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_head");
        CmsSiteModule footer = cmsSiteModuleService.selectCmsSiteModuleTypeValue(info.getModuleType(), "m_footer");
        model.addAttribute("info", info);
        model.addAttribute("head", head);
        model.addAttribute("footer", footer);
        return info;
    }

    @GetMapping("/index")
    public String index(Model model, @RequestParam(value = "searchKey", required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            //进行文章搜索
            if (!StringUtils.isEmpty(searchKey)) {    //不为空则进行条件查询
                paramBanner.setTitle(searchKey);
            }
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }

    @GetMapping("/blog")
    public String blog(Model model, @RequestParam(value = "searchKey", required = false) String searchKey) {


        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            //进行文章搜索
            if (!StringUtils.isEmpty(searchKey)) {    //不为空则进行条件查询
                paramBanner.setTitle(searchKey);
            }
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "blog";
    }

    @GetMapping("/about")
    public String about(Model model, @RequestParam(value = "searchKey", required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            //进行文章搜索
            if (!StringUtils.isEmpty(searchKey)) {    //不为空则进行条件查询
                paramBanner.setTitle(searchKey);
            }
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "about";
    }

    @GetMapping("/contact")
    public String contact(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "contact";
    }


    @PostMapping("/cms_contact_addly")
    @ResponseBody
    public Map addly(CmsSiteLy cmsSiteLy) {
        int i = cmsSiteLyService.insertCmsSiteLy(cmsSiteLy);
        HashMap<Object, Object> hashMap = new HashMap<>();
        if (i > 0) {
            hashMap.put("code", i);
            hashMap.put("msg", "留言成功");
        } else {
            hashMap.put("code", i);
            hashMap.put("msg", "留言失败");
        }

        return hashMap;
    }

    @GetMapping("/cms_detail")
    public String detail(Model model, @RequestParam(value = "id", required = true) String id) {
        CmsSiteBanner cmsSiteBanner = cmsSiteBannerService.selectCmsSiteBannerById(Long.parseLong(id));
        model.addAttribute("data", cmsSiteBanner);
        return "detail";
    }

    @GetMapping("/cms_blog_page")
    @ResponseBody
    public TableDataInfo blogPage() {
        startPage();
        CmsSiteBanner paramBanner = new CmsSiteBanner();
        paramBanner.setModuleId(37L);
        List<CmsSiteBanner> cmsSiteBanners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);

        TableDataInfo dataTable = getDataTable(cmsSiteBanners);
        System.out.println(dataTable);

        return dataTable;
    }

}
